Find perfect squares between two given numbers¶
Find perfect squares between two given numbers.
Expected output:
[1, 4, 9, 16, 25, …]
def squares(a, b):
L = []
# Traverse through all numbers
for i in range (a, b + 1):
j = 1;
while j * j <= i:
if j * j == i:
L.append(i)
j = j + 1
i = i + 1
return L
# test
print(squares(1, 30))
Output:
[1, 4, 9, 16, 25]